package com.link.base.base.contractsinfo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.contractsinfo.model.ContractApprovalInfo;
import com.link.base.base.contractsinfo.model.ContractsInfo;
import com.link.base.base.contractsinfo.service.ContractsInfoService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liushoulong
 * @date 2018-11-05
 * @desc 合同信息
 */
@Controller("ContractsInfoController")
@RequestMapping("/link/contractsInfo")
public class ContractsInfoController extends BasicController<ContractsInfo> {
    @Resource
    private ContractsInfoService contractsInfoService;

    @Override
    public BasicService<ContractsInfo> getBasicService() throws Exception {
        return contractsInfoService;
    }

    /**
     * 合同签约提交审批
     *
     * @param entity
     * @return Map
     */
    @RequestMapping("contractSignSubmit")
    @ResponseBody
    public Map<String, Object> contractSignSubmit(@RequestBody ContractApprovalInfo entity, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(6);
        ObjectMapper mapper = new ObjectMapper();
        try {
            /*审批详情封装json字符串*/
            String approvalDetail = mapper.writeValueAsString(entity);
            if (null == entity.getContractId()) {
                throw new ServiceException("CONTRACTSINFO-001");
            }
            ContractsInfo contract = new ContractsInfo();
            contract.setId(entity.getContractId());
            /*查找当前客户*/
            ContractsInfo contractObj = contractsInfoService.queryById(contract);
            if (contractObj == null) {
                throw new ServiceException("CONTRACTSINFO-002");
            }
            contractsInfoService.contractSignSubmit(contractObj, approvalDetail);
            ContractsInfo newRow = contractsInfoService.queryById(contractObj);
            result.put("success", true);
            result.put("newRow", newRow);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 合同签约审批处理-审批成功后更新客户信息和合同信息
     *
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/contractSignObjectUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> contractSignObjectUpdate(@RequestBody ContractApprovalInfo entity, HttpSession session,
                                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            /*更新信息*/
            contractsInfoService.contractSignObjectUpdate(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
